package com.bosicloud.cmb.ibp.commom.dto;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @author Jack Peng / ziliang.peng@bosicloud.com
 */
@Data
@Builder
public class UnionResult<T> implements Serializable {

    public enum StatusEnum {
        /**
         * 成功
         */
        Sucess(200),
        /**
         * 系统降级
         */
        HystrixDegradation(300),
        /**
         * 熔断
         */
        HystrixFusing(301),
        /**
         * 内部错误
         */
        InnerError(500),
        /**
         * 无权访问
         */
        Forbidden(403),
        /**
         * 业务异常
         */
        Fail(420),
        /**
         * 未发现Token
         */
        FailToken(401),
        /**
         * 用户名或密码错误
         */
        FailLogin(402),
        /**
         * 验证码错误
         */
        FailVCode(403),
        /**
         * 资源不存在
         */
        NotFound(404),
        /**
         * 表单错误
         */
        FormFail(405),
        /**
         * 未知错误
         */
        Unknow(0),
        /**
         * 参数错误
         */
        ParamsFail(406);

        private int code;

        StatusEnum(int code) {
            this.code = code;
        }

        public int code() {
            return code;
        }
    }


    /**
     * 状态
     */
    private Integer status;

    /**
     * 返回的数据
     */
    private T data;


    /**
     * 消息,通常是错误状态下的错误消息
     */
    private String msg;

    /**
     * 时间
     */
    private Date time = new Date();
    /**
     * 2个扩展数据
     */
    private Object x1;
    private Object x2;

    public UnionResult() {}

    public UnionResult(T data) {
        this.status = StatusEnum.Sucess.code();
        this.data = data;
    }

    public UnionResult(StatusEnum status, T data) {
        this.status = status.code();
        this.data = data;
    }

    public UnionResult(int status, T data) {
        this.status = status;
        this.data = data;
    }

    public UnionResult(StatusEnum status, String msg) {
        this.status = status.code();
        this.msg = msg;
    }

    public UnionResult(Integer status, T data, String msg, Date time, Object x1, Object x2) {
        this.status = status;
        this.data = data;
        this.msg = msg;
        this.time = time;
        this.x1 = x1;
        this.x2 = x2;
    }

}
